home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Application / Application.cp next >
Encoding:
Text File  |  1997-06-28  |  1.5 KB  |  88 lines  |  [TEXT/CWIE]

  1. // Application.cp
  2.  
  3. #ifndef Application_h
  4. #include "Application.h"
  5. #endif
  6. #ifndef EventQueue_h
  7. #include "EventQueue.h"
  8. #endif
  9. #ifndef RootFocus_h
  10. #include "RootFocus.h"
  11. #endif
  12. #ifndef Event_h
  13. #include "Event.h"
  14. #endif
  15. #ifndef Process_h
  16. #include "Process.h"
  17. #endif
  18. #ifndef UserState_h
  19. #include "UserState.h"
  20. #endif
  21. #ifndef UserCancelledError_h
  22. #include "UserCancelledError.h"
  23. #endif
  24. #ifndef BroadcastLoop_h
  25. #include "BroadcastLoop.h"
  26. #endif
  27.  
  28. Application *Application::the = 0;
  29.  
  30. Application::Application()
  31.   : CommandHandler<Quitting>( RootFocus::The() ),
  32.      quitting( false )
  33.   {
  34.     Assert( the == 0 );
  35.     the = this;
  36.   }
  37.  
  38. Application::~Application()
  39.   {
  40.     if ( RootFocus::The().Active() )
  41.         RootFocus::Deactivate();
  42.   }
  43.  
  44. void Application::Run()
  45.   {
  46.     EventManagerUser();
  47.     if ( Process::Application() == Process::Front() )
  48.         RootFocus::Activate();
  49.  
  50.     UserState::The().Announce();
  51.     
  52.     EventQueue& queue( EventQueue::The() );
  53.     
  54.     for ( queue++; !quitting; queue++ )
  55.       {
  56.         Event& event( MakeEvent( *queue ) );
  57.         
  58.         while ( event.CanAppend() )
  59.           {
  60.             queue.AdvanceWithoutWaiting();
  61.             if ( !event.Append( *queue ) )
  62.               {
  63.                 queue--;
  64.                 break;
  65.               }
  66.           }
  67.         
  68.         event.Respond();
  69.       }
  70.   }
  71.  
  72. bool Application::CanQuit() const
  73.   {
  74.     return true;
  75.   }
  76.  
  77. void Application::Quit( SavingOption savingOption )
  78.   {
  79.     for ( BroadcastLoop<PreparingToQuit> receiver( *this );
  80.             receiver.Unfinished();
  81.             receiver++ )
  82.         receiver->PrepareToQuit( savingOption );
  83.     
  84.     quitting = true;
  85.   }
  86.  
  87. #include "BroadcastLoop.cp"
  88.